/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/comm",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",
    "scprit/assess/app/monthlyAssess/monthlyAssessList.ui.js"],
    function($,$bb,$comm,$datatableServers,$datatableUi,$ui){
	var servers = {};
	var URL = $bb.baseUrl("followup");
    var URL2 = $bb.baseUrl("assess");

    /**
     * 请求队列成功后的回调函数
     */
    var ajaxQueuesCallBack = function(rows){
        $comm.selectQueue({
            "id": "queueId", 
            "list": rows
        });
    }

    /**
     * 加载队列
     */
    var ajaxQueues = function(Callback){
        $bb.ajax({
            url: URL + "followup/service/queue/followQueueListQuery",
            success: function (data) {
                if(data.success){
                    Callback(data.data.rows);
                }
            }
        });
    }

    /**
     * 请求修改状态成功后的回调函数
     */
    var statusModifyCallBack = function(data){
        $("#modalBox .myModal").modal("hide");
        init();
    }

    /**
     * 请求修改状态
     */
    var statusModify = function(params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/workorder/modify",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){ 
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请求修改状态出错！");
                }
            }
        });
    }

    /**
     * 状态修改
     */
    var onStatusModify = function(){
        var workOrderId = $(this).data("workOrderId");
        var status = $("#statusSelect").val();
        var params = {
            "status":status,
            "workOrderId":workOrderId
        }
        statusModify(params,statusModifyCallBack);
    }

    /**
     * 弹出修改状态模态框
     */
    var onChangeStatus = function(){
        var status = $(this).data("status");
        var workOrderId = $(this).data("workOrderId");
        var list = [
            {"key":"01","text":"未处理"},
            {"key":"02","text":"已处理"},
            {"key":"03","text":"待医生确认"},
            {"key":"04","text":"医生已确认"},
            {"key":"05","text":"已发送患者"},
            {"key":"06","text":"患者已确认"}
        ];
        $comm.modal({
            "tit":"改变状态"
        },function(){
            $ui.changeStatusInit({
                "$modalBody":$("#modalBox .myModal .modal-body"),
                "data":{
                    "status":status,
                    "list":list
                }
            });
            $("#modalBox .myModal").modal("show");
            $("#statusModifyBtn").unbind("click");
            $("#statusModifyBtn").data({"workOrderId":workOrderId});
            $("#statusModifyBtn").click(onStatusModify);
        });
    }

    /**
     * 初始化
     */
    
    var init = function(){
        ajaxQueues(ajaxQueuesCallBack);    // 加载队列
        $ui.tableInit({
            "id": "monthlyAssessListBox",
        });
        $datatableUi.init({
            "id": "monthlyAssessList",
            "options": getOptions()
        });
    }
    servers.init = function(){
        init();
    }

    /******************************************************************************/

    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "phoneNo": $.trim($("#phoneNo").val()),
                "queueId": $("#queueId").val(),
                "status": $.trim($("#status").val()),
                "userName": $.trim($("#userName").val()),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsGender = function(e){
        return e.gender == "F" ? "女" : "男";
    }
    var columnsStatus = function(e){
        var obj = {};
        switch(e.status){
            case "01":
                obj.data = "未处理";
                obj.className = "status-01";
                break;
            case "02":
                obj.data = "已处理";
                obj.className = "status-02";
                break;
            case "03":
                obj.data = "待医生确认";
                obj.className = "status-03";
                break;
            case "04":
                obj.data = "医生已确认";
                obj.className = "status-04";
                break;
            case "05":
                obj.data = "已发送患者";
                obj.className = "status-05";
                break;
            case "06":
                obj.data = "患者已确认";
                obj.className = "status-06";
                break;
        }
        var h = '<div class="' + obj.className + '">' + obj.data + '</div>';
        return h;
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":'workOrderId'},
            {"data":'phoneNo'},
            {"data":'userName'},
            {"data": columnsGender},
            {"data":'weight'},
            {"data":'birthday'},
            {"data":'pregnancyDay'},
            {"data":'createTime'},
            {
                "data": columnsStatus,
                "className":"status"
            },
            {
                "data":'doctorName',
                "className":"doctor-name"
            },
            {"data":'dealTime'},
            {
                "data": null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }

    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        $('td.operate', row).html('<div class="btn-look">查看</div>');
        $('td.operate', row).find(".btn-look").click(function () {
            $bb.openPage("monthlyAssessDetail",{"workOrderId":data.workOrderId});
        });
        $('td.status', row).unbind("click");
        $('td.status', row).data({"status":data.status,"workOrderId":data.workOrderId});
        $('td.status', row).click(onChangeStatus);
        return row;
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#monthlyAssessList").prev("div.row").remove();
        $("#serchBtn").click(function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数 
        });
        $("#resetBtn").click(function(){
            $("#userName,#phoneNo,#queueId,#status").val("");
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    var getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/evaluation/workorder/list?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});